import logging

import pytest

from resource.pom.settings import Settings
from utils.manager import Images
from resource.pom.system_ui import SystemUI


class Test:
    system_ui = SystemUI
    app = Settings

    @pytest.mark.parametrize('setup_teardown', [app.bundle_name], indirect=True)
    def test(self, setup_teardown, device):
        logging.info('步骤1：进入桌面，右上角下划进入控制中心，点击控制中心位置信息按钮')
        device.start_ability(self.app.bundle_name, self.app.ability_name)
        device.refresh_layout()
        privacy = device.get_element_by_text('隐私')
        device.click_element(privacy)
        device.refresh_layout()
        locate = device.get_element_by_text('定位服务')
        device.click_element(locate)
        device.refresh_layout()
        status0 = device.get_element_by_type('Toggle')
        x = 0
        y = 0
        for i in range(2):
            logging.info('步骤1.{}：点击控制中心[位置信息]按钮'.format(i))
            device.dropdown_controller_center()
            if x == 0 and y == 0:
                device.refresh_layout()
                locale = device.get_element_by_text('位置信息')
                x, y = device.center_of_element(locale)
            device.wait(1)
            device.click(x, y - 75)
            device.wait(5)
            # 收起控制中心进设置看定位
            device.swipe_up_dropdown_pan()
            logging.info('预期结果1.{}：点击或重复点击有效，与设置隐私的位置开关同步变化，开启时最上侧有位置标识，无异常'.format(i))
            device.refresh_layout()
            if not status0['checked']:
                status_bar = device.save_snapshot_to_local('statusbar.jpeg')
                Images.crop_picture(status_bar, *self.system_ui.statusbar_area)
                assert Images.is_image_in_image(self.system_ui.locate_icon, status_bar), '状态栏没有位置图标'
            status1 = device.get_element_by_type('Toggle')
            logging.debug(status0)
            logging.debug(status1)
            assert status0.get('checked') != status1.get('checked'), '位置开关状态变化无效'
            status0 = status1
